<template>
  <div class="dashboard-container">
    <h1>学生Dashboard</h1>
    <div class="welcome-message">
      <p>你好，{{ userProfile.fullName }}同学！</p>
      <p>欢迎使用家校共创系统。</p>
    </div>
    
    <div class="user-info" v-if="userProfile">
      <h2>基本信息</h2>
      <p>姓名: {{ userProfile.fullName }}</p>
      <p>学号: {{ userProfile.studentNumber }}</p>
      <p>性别: {{ userProfile.gender }}</p>
      <p>出生日期: {{ formatDate(userProfile.dateOfBirth) }}</p>
      <p>班级: {{ userProfile.className }}</p>
      <p>入学日期: {{ formatDate(userProfile.enrollmentDate) }}</p>
      <p>学籍状态: {{ userProfile.status }}</p>
      <p>联系电话: {{ userProfile.phoneNumber || '未填写' }}</p>
    </div>
    
    <div class="parent-info" v-if="userProfile.parentInfos && userProfile.parentInfos.length > 0">
      <h2>家长信息</h2>
      <div v-for="(parent, index) in userProfile.parentInfos" :key="index" class="parent-card">
        <p>姓名: {{ parent.fullName }}</p>
        <p>关系: {{ parent.relationship }}</p>
        <p>联系电话: {{ parent.phoneNumber || '未填写' }}</p>
      </div>
    </div>
    <div class="parent-info" v-else-if="userProfile">
      <h2>家长信息</h2>
      <p>暂无家长信息</p>
    </div>
    
    <div class="navigation">
      <router-link to="/student/clock" class="nav-button">考勤打卡</router-link>
      <router-link to="/student/leave" class="nav-button">请假申请</router-link>
      <router-link to="/student/records" class="nav-button">考勤记录</router-link>
      <router-link to="/student/events" class="nav-button">活动报名</router-link>
      <router-link to="/student/homework" class="nav-button">我的作业</router-link>
      <button @click="handleLogout" class="logout-button">退出登录</button>
    </div>
    
    <div v-if="error" class="error-message">
      {{ error }}
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';

export default {
  name: 'StudentDashboard',
  setup() {
    const router = useRouter();
    const userProfile = ref({});
    const error = ref('');
    
    // 获取用户信息
    const loadUserProfile = async () => {
      try {
        const response = await fetch('/user/profile', {
          method: 'GET',
          credentials: 'include',
          headers: {
            'Content-Type': 'application/json'
          }
        });
        
        const result = await response.json();
        if (result.code === 1) {
          userProfile.value = result.data;
        } else {
          error.value = result.msg || '获取用户信息失败';
        }
      } catch (err) {
        error.value = '网络错误，请稍后重试';
      }
    };
    
    // 格式化日期
    const formatDate = (dateString) => {
      if (!dateString) return '';
      const date = new Date(dateString);
      return date.toLocaleDateString('zh-CN');
    };
    
    // 退出登录
    const handleLogout = async () => {
      try {
        const response = await fetch('/user/logout', {
          method: 'POST',
          credentials: 'include',
          headers: {
            'Content-Type': 'application/json'
          }
        });
        
        const result = await response.json();
        if (result.code === 1) {
          // 退出成功，跳转到登录页
          router.push('/login');
        } else {
          error.value = result.msg || '退出登录失败';
        }
      } catch (err) {
        error.value = '网络错误，请稍后重试';
      }
    };
    
    onMounted(() => {
      loadUserProfile();
    });
    
    return {
      userProfile,
      error,
      formatDate,
      handleLogout
    };
  }
};
</script>

<style scoped>
.dashboard-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  font-family: Arial, sans-serif;
}

.dashboard-container h1 {
  text-align: center;
  color: #333;
}

.welcome-message {
  background-color: #e8f4fd;
  padding: 20px;
  border-radius: 5px;
  text-align: center;
  margin-bottom: 20px;
}

.user-info, .parent-info {
  background-color: #f9f9f9;
  padding: 20px;
  border-radius: 5px;
  margin-bottom: 20px;
}

.user-info h2, .parent-info h2 {
  margin-top: 0;
}

.parent-card {
  background-color: #fff;
  padding: 15px;
  border-radius: 5px;
  margin-bottom: 10px;
  border: 1px solid #eee;
}

.parent-card:last-child {
  margin-bottom: 0;
}

.navigation {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-top: 30px;
  flex-wrap: wrap;
}

.nav-button, .logout-button {
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 16px;
  text-decoration: none;
  display: inline-block;
  text-align: center;
  min-width: 120px;
}

.nav-button {
  background-color: #409EFF;
  color: white;
}

.nav-button:hover {
  background-color: #337ecc;
}

.logout-button {
  background-color: #F56C6C;
  color: white;
}

.logout-button:hover {
  background-color: #e05858;
}

.error-message {
  background-color: #f8d7da;
  color: #721c24;
  padding: 15px;
  border-radius: 5px;
  margin-top: 20px;
}
</style>